package com.cx.service.wjr.impl;

import com.cx.mapper.CourierMapper;
import com.cx.mapper.CourierWorkMapper;
import com.cx.mapper.PageMapper;
import com.cx.pojo.*;
import com.cx.pojo.wjr.UserPage;
import com.cx.redis.RedisOpsUtil;
import com.cx.service.wjr.CourierService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;
@Service
public class CourierServiceImpl implements CourierService {
    @Resource
    PageMapper pageMapper;

    @Resource
    CourierMapper courierMapper;

    @Resource
    CourierWorkMapper courierWorkMapper;
    @Autowired
    RedisOpsUtil redisOpsUtil;


    @Override
    public List<Courier> selectLogin(CourierExample courierExample) {
        return courierMapper.selectByExample(courierExample);
    }

    @Override
    public Courier selectCourierById(Integer id) {
        return courierMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<UserPage> selectGrabbing(UserPage userPage) {
        
        return pageMapper.selectGrabbing(userPage);
    }

    @Override
    public List<UserPage> selectNew(UserPage userPage) {

        return pageMapper.selectNew(userPage);
    }


    public List<UserPage> selectRedisPage(UserPage userPage) {
        List<UserPage> userPages = pageMapper.selectGrabbing(userPage);
        for (UserPage userPageRedis: userPages) {
            int id = userPageRedis.getPageId();
            if(redisOpsUtil.exists(id + "")){
                userPage = redisOpsUtil.get(id+"", UserPage.class);
            } else  {
                Page page  = pageMapper.selectByPrimaryKey(id);
                redisOpsUtil.set(id+"", page, 120);
            }
        }
        return userPages;
    }

    @Override
    @Transactional(propagation =  Propagation.REQUIRED)
    public int courierUpdate(PageExample pageExample,Page page) {
        System.out.println(page);
        System.out.println("1111111");
        List<Page> pageList = pageMapper.selectByExample(pageExample);
        pageList.forEach(System.out::println);
        try{
        if(pageList.size()>0){
            if(pageList.get(0).getStatus()==page.getStatus()){
                throw new RuntimeException("订单状态已更新");
            }

        }
            //出现异常
        } catch (Exception e) {
            e.printStackTrace();
            //设置手动回滚
            TransactionAspectSupport.currentTransactionStatus()
                    .setRollbackOnly();
            return 0;
        }
        return pageMapper.updateByExampleSelective(page,pageExample);


    }



    @Override
    public int courierInCome(CourierWork courierWork, CourierWorkExample courierWorkExample) {
        return courierWorkMapper.updateByExampleSelective(courierWork,courierWorkExample);
    }

    @Override
    public List<CourierWork> courierInHowInCome(CourierWorkExample courierWorkExample) {
        return courierWorkMapper.selectByExample(courierWorkExample);
    }
}
